package com.atguigu.gmall.realtime.util;

import com.atguigu.gmall.realtime.bean.OrderInfo;
import com.atguigu.gmall.realtime.common.Constant;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/9/29 10:25
 */
public class JdbcUtil {
    
    
    public static Connection getPhoenixConnection(String url) {
        String driver = Constant.PHOENIX_DRIVER;
        return getJdbcConnection(driver, url);
    }
    
    public static Connection getMysqlConnection(String url) {
    
        return getJdbcConnection(Constant.MYSQL_DRIVER, url);
    }
    
    public static Connection getJdbcConnection(String driver, String url) {
        try {
            Class.forName(driver); // 加载驱动
            return DriverManager.getConnection(url); // 获取连接
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public static void main(String[] args) {
        List<OrderInfo> list =
            queryList(getMysqlConnection(Constant.MYSQL_URL), "select * from order_info", new String[]{}, OrderInfo.class);
    
        for (OrderInfo obj : list) {
            System.out.println(obj);
        }
    }
    
    public static <T> List<T> queryList(Connection conn,
                                        String sql,
                                        Object[] args,
                                        Class<T> tClass) {
        // User(id, name)
        
        List<T> result = new ArrayList<>();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            // 给sql的占位符进行赋值
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            /*
            id  name
            1    zs
            2    lisi
             */
            
            ResultSet resultSet = ps.executeQuery();
            
            // 获取元数据信息: 列名, 列的类型, 列的个数, ...
            ResultSetMetaData metaData = resultSet.getMetaData();
            
            while (resultSet.next()) { // 遍历行
                T t = tClass.newInstance();  // new User()
                // 遍历列
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    // 取出列名:   对应着 T类型的属性名  (T 是Map , 对应着key)
                    String columnName = metaData.getColumnLabel(i);
                    // 取出列值: 给 T 中的属性赋值
                    Object value = resultSet.getObject(columnName);
                    BeanUtils.setProperty(t, columnName, value);
                }
                result.add(t);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return result;
    }
}
